//
//  ReportMatePopView.m
//  TQY
//
//  Created by 甘世清 on 2024/10/9.
//

#import "ReportMatePopView.h"
#import "MateTypeCollectionViewCell.h"
#import "MateTypeModel.h"
@implementation ReportMatePopView
 
- (void)SQ_UIViewForSelfView{
    self.backgroundColor = SQColorF1F1F1;
    self.layer.cornerRadius = 30;
    
    UILabel * label = [[UILabel alloc]init];
    label.text = SQNSLocalString(@"举报");
    label.textColor = SQColor333333;
    label.font = FONT_MEDIUM(16);
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(24);
    }];
    [self.collectionView registerClass:[MateTypeCollectionViewCell class] forCellWithReuseIdentifier:@"MateTypeCollectionViewCell"];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
        make.bottom.equalTo(self.mas_bottom).offset(-FootHeight-30);
            make.top.equalTo(self).offset(54);
    }];
    
    [self addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.mas_equalTo(SCREEN_WIDETH-40);
            make.height.mas_equalTo(50);
            make.bottom.equalTo(self.mas_bottom).offset(-FootHeight-10);
    }];
    self.addButton.backgroundColor = SQHexColor(@"#3DA59D", 0.4);
    self.addButton.textColor = SQColor(@"#666666");
    self.addButton.SQTouchBtn.hidden = YES;
    WS(weakSelf);
    [self.addButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_addBtnClick];
    }];
    [self SQ_Header];
}
-(void)SQ_addBtnClick{
    WS(weakSelf);
    NSMutableArray * array = [[NSMutableArray alloc]init];
    for (MateTypeModel * model in self.jsonArray) {
        if (model.isSeleted) {
            [array addObject:model];
        }
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.uid forKey:@"pid"];
    [dic setObject:@"0" forKey:@"type"];
    [dic setObject:[array componentsJoinedByString:@","] forKey:@"content"];
    [HttpTool post:@"report/save" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD  showMsgWithoutView:SQNSLocalString(@"举报成功")];
        if (self.reportMatetouchBlock) {
            self.reportMatetouchBlock();
        }  
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (void)SQ_Header{
    NSArray * array = @[@"低俗色情",@"侮辱谩骂",@"违法行为",@"政治敏感",@"垃圾广告",@"未成年有害",@"不尊重女性",@"制造冲突",@"恶意诋毁",@"感官不适",@"网络暴力",@"其他原因"];
    for (NSString * name  in array) {
        MateTypeModel * model = [[MateTypeModel alloc]init];
        model.string = name;
        [self.jsonArray addObject:model];
    }
    [self.collectionView reloadData];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    MateTypeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MateTypeCollectionViewCell" forIndexPath:indexPath];
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((SCREEN_WIDETH-28)/3.f, 40);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 0, 10);
}
 
 //点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MateTypeModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    model.isSeleted = !model.isSeleted;
    [self.collectionView reloadData];
    NSMutableArray * array = [[NSMutableArray alloc]init];
    for (MateTypeModel * model in self.jsonArray) {
        if (model.isSeleted) {
            [array addObject:model];
        }
    }
    if (array.count > 0) {
        self.addButton.backgroundColor = SQColor3DA59D;
        self.addButton.textColor = SQColor(@"#FFFFFF");
        self.addButton.SQTouchBtn.hidden = NO;
    } else {
        self.addButton.backgroundColor = SQHexColor(@"#3DA59D", 0.4);
        self.addButton.textColor = SQColor(@"#666666");
        self.addButton.SQTouchBtn.hidden = YES;
    }
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    
    }
    return _collectionView;
}

-(NSMutableArray*)jsonArray
{
    if (!_jsonArray) {
        _jsonArray = [[NSMutableArray alloc]init];
    }
    return _jsonArray;
}

- (SQCustomButton *)addButton{
    if (!_addButton) {
        _addButton = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _addButton.backgroundColor = SQColor3DA59D;
        _addButton.textColor = SQColor(@"#FFFFFF");
        _addButton.layer.cornerRadius = 25;
        _addButton.layer.masksToBounds = YES;
        _addButton.text = SQNSLocalString(@"确认");
        _addButton.font = FONT_MEDIUM(16);
    }
    return _addButton;
}

@end
